package db;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JSeparator;
import javax.swing.ImageIcon;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class List extends JPanel {
	private JScrollPane scrollPane;
	private JTextField text;
	private JButton btnNewButton;
	private JTable table;
	DefaultTableModel model;
	DefaultTableModel mod;
	Vector<String> col = new Vector<String>();
	static Connection con;
	ButtonGroup bg = new ButtonGroup();
	Vector<String> vec = new Vector<String>();
	private JTextField text1;
	private JLabel lblNewLabel;
	private JTextField text2;
	private JLabel label;
	private JTextField text3;
	private JLabel label_1;
	private JTextField text4;
	private JLabel label_2;
	private JTextField text5;
	private JLabel label_3;
	private JTextField text6;
	private JLabel label_4;
	private JTextField text7;
	private JLabel label_5;
	private JTextField text8;
	private JLabel label_6;
	private JComboBox box;
	private JLabel lblNewLabel_1;
	private JSeparator separator;
	private JLabel lblhta;

	public List(Connection c) {
		setBackground(Color.BLACK);
		this.con = c;
		col.add("제품코드");
		col.add("제품이름");
		col.add("제품원가");
		col.add("등록일");
		col.add("등록자");
		col.add("수정일");
		col.add("수정자");
		col.add("사업처");
		model = new DefaultTableModel(col, 0) {
			public boolean isCellEditable(int rowIndex, int mColIndex) {
				return false;
			}
		};
		setLayout(null);
		add(getScrollPane());
		add(getText());
		add(getBtnNewButton());
		add(getText1());
		add(getLblNewLabel());
		add(getText2());
		add(getLabel());
		add(getText3());
		add(getLabel_1());
		add(getText4());
		add(getLabel_2());
		add(getText5());
		add(getLabel_3());
		add(getText6());
		add(getLabel_4());
		add(getText7());
		add(getLabel_5());
		add(getText8());
		add(getLabel_6());

		add(getSeparator());
		add(getLblNewLabel_1());
		add(getLblhta());
		table.setRowHeight(25);
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(28, 109, 408, 285);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTextField getText() {
		if (text == null) {
			text = new JTextField();
			text.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER) {
						search();
					}
				}
			});
			text.setBounds(28, 67, 309, 28);
			text.setColumns(10);
		}
		return text;
	}

	public void MouseC() {
		int index = table.getSelectedRow();
		text1.setText(table.getValueAt(index, 0) + "");
		text2.setText(table.getValueAt(index, 1) + "");
		text3.setText(table.getValueAt(index, 2) + "");
		text4.setText(table.getValueAt(index, 3) + "");
		text5.setText(table.getValueAt(index, 4) + "");
		text6.setText(table.getValueAt(index, 5) + "");
		text7.setText(table.getValueAt(index, 6) + "");
		text8.setText(table.getValueAt(index, 7) + "");
	}

	private boolean search() {
		try {
			String str = text.getText();
			model.setRowCount(0);
			Statement stmt = con.createStatement();

			// 가져올 자료 요청
			String sql = "select * from code where pro_code like '%" + str
					+ "%' order by pro_code asc";
			ResultSet RS = stmt.executeQuery(sql);
			while (RS.next()) {
				Vector<String> row = new Vector<String>();
				row.add(RS.getString(1));
				row.add(RS.getString(2));
				row.add(RS.getString(3));
				row.add(RS.getString(4));
				row.add(RS.getString(5));
				row.add(RS.getString(6));
				row.add(RS.getString(7));
				row.add(RS.getString(8));
				model.addRow(row);
			}
			RS.close();
			stmt.close();
			return true;
		} catch (Exception e) {
			System.out.println("예외사항:" + e.getMessage());
			return false;
		}
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조 회");
			btnNewButton.setBackground(Color.BLACK);
			btnNewButton.setForeground(Color.WHITE);
			btnNewButton.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 30));
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					search();
				}
			});
			btnNewButton.setBounds(349, 66, 93, 28);
		}
		return btnNewButton;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.setFont(new Font("HY동녘B", Font.PLAIN, 14));
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					MouseC();
				}
			});
		}
		return table;
	}

	private JTextField getText1() {
		if (text1 == null) {
			text1 = new JTextField();
			text1.setMinimumSize(new Dimension(8, 25));
			text1.setPreferredSize(new Dimension(8, 25));
			text1.setEditable(false);
			text1.setBounds(541, 109, 116, 21);
			text1.setColumns(10);
		}
		return text1;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("제품 코드");
			lblNewLabel.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			lblNewLabel.setForeground(Color.WHITE);
			lblNewLabel.setBounds(459, 109, 70, 21);
		}
		return lblNewLabel;
	}

	private JTextField getText2() {
		if (text2 == null) {
			text2 = new JTextField();
			text2.setMinimumSize(new Dimension(8, 25));
			text2.setPreferredSize(new Dimension(8, 25));
			text2.setEditable(false);
			text2.setColumns(10);
			text2.setBounds(541, 140, 116, 21);
		}
		return text2;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("제품 이름");
			label.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label.setForeground(Color.WHITE);
			label.setBounds(459, 140, 70, 21);
		}
		return label;
	}

	private JTextField getText3() {
		if (text3 == null) {
			text3 = new JTextField();
			text3.setMinimumSize(new Dimension(8, 25));
			text3.setPreferredSize(new Dimension(8, 25));
			text3.setEditable(false);
			text3.setColumns(10);
			text3.setBounds(541, 171, 116, 21);
		}
		return text3;
	}

	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("제품 원가");
			label_1.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_1.setForeground(Color.WHITE);
			label_1.setBounds(459, 171, 70, 21);
		}
		return label_1;
	}

	private JTextField getText4() {
		if (text4 == null) {
			text4 = new JTextField();
			text4.setMinimumSize(new Dimension(8, 25));
			text4.setPreferredSize(new Dimension(8, 25));
			text4.setEditable(false);
			text4.setColumns(10);
			text4.setBounds(541, 202, 116, 21);
		}
		return text4;
	}

	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("입고일");
			label_2.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_2.setForeground(Color.WHITE);
			label_2.setBounds(459, 202, 57, 21);
		}
		return label_2;
	}

	private JTextField getText5() {
		if (text5 == null) {
			text5 = new JTextField();
			text5.setMinimumSize(new Dimension(8, 25));
			text5.setPreferredSize(new Dimension(8, 25));
			text5.setColumns(10);
			text5.setBounds(541, 233, 116, 21);
		}
		return text5;
	}

	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("수령자");
			label_3.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_3.setForeground(Color.WHITE);
			label_3.setBounds(459, 233, 57, 21);
		}
		return label_3;
	}

	private JTextField getText6() {
		if (text6 == null) {
			text6 = new JTextField();
			text6.setMinimumSize(new Dimension(8, 25));
			text6.setPreferredSize(new Dimension(8, 25));
			text6.setEditable(false);
			text6.setColumns(10);
			text6.setBounds(541, 264, 116, 21);
		}
		return text6;
	}

	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("수정일");
			label_4.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_4.setForeground(Color.WHITE);
			label_4.setBounds(459, 264, 57, 21);
		}
		return label_4;
	}

	private JTextField getText7() {
		if (text7 == null) {
			text7 = new JTextField();
			text7.setMinimumSize(new Dimension(8, 25));
			text7.setPreferredSize(new Dimension(8, 25));
			text7.setColumns(10);
			text7.setBounds(541, 295, 116, 21);
		}
		return text7;
	}

	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("수정자");
			label_5.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_5.setForeground(Color.WHITE);
			label_5.setBounds(459, 295, 57, 21);
		}
		return label_5;
	}

	private JTextField getText8() {
		if (text8 == null) {
			text8 = new JTextField();
			text8.setMinimumSize(new Dimension(8, 25));
			text8.setPreferredSize(new Dimension(8, 25));
			text8.setEditable(false);
			text8.setColumns(10);
			text8.setBounds(541, 326, 116, 21);
		}
		return text8;
	}

	private JLabel getLabel_6() {
		if (label_6 == null) {
			label_6 = new JLabel("거래처");
			label_6.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_6.setForeground(Color.WHITE);
			label_6.setBounds(459, 326, 70, 21);
		}
		return label_6;
	}

	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("제품 조회");
			lblNewLabel_1.setBounds(0, 0, 679, 57);
			lblNewLabel_1.setPreferredSize(new Dimension(700, 25));
			lblNewLabel_1.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 50));
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_1.setForeground(Color.WHITE);
		}
		return lblNewLabel_1;
	}

	private JSeparator getSeparator() {
		if (separator == null) {
			separator = new JSeparator();
			separator.setPreferredSize(new Dimension(380, 5));
			separator.setBounds(22, 51, 679, 5);
		}
		return separator;
	}

	private JLabel getLblhta() {
		if (lblhta == null) {
			lblhta = new JLabel("중앙HTA 할인매장 - 5조");
			lblhta.setFont(new Font("나눔손글씨 펜", Font.BOLD, 25));
			lblhta.setForeground(Color.WHITE);
			lblhta.setBounds(474, 455, 218, 28);
		}
		return lblhta;
	}
}